-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
59 lines (49 loc) · 1.58 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Item {
int weight;
int value;
double ratio;
};
bool compareItems(const Item &a, const Item &b) {
// Sort in descending order of ratio
return a.ratio > b.ratio;
}
int main() {
int n;
cout << "Enter the number of items: ";
cin >> n;
vector<Item> items(n);
for (int i = 0; i < n; i++) {
cout << "Enter value and weight for item " << i + 1 << ": ";
cin >> items[i].value >> items[i].weight;
items[i].ratio = (double) items[i].value / items[i].weight;
}
int capacity;
cout << "Enter the capacity of the knapsack: ";
cin >> capacity;
// Sort items based on value-to-weight ratio in descending order
sort(items.begin(), items.end(), compareItems);
double totalValue = 0.0;
vector<double> fraction(n, 0.0);
// Greedy selection: take full item if possible, else take fraction
for (int i = 0; i < n && capacity > 0; i++) {
if (items[i].weight <= capacity) {
fraction[i] = 1.0;
totalValue += items[i].value;
capacity -= items[i].weight;
} else {
fraction[i] = (double) capacity / items[i].weight;
totalValue += items[i].value * fraction[i];
capacity = 0;
}
}
cout << "\nMaximum value achievable: " << totalValue << endl;
cout << "Item fractions taken:" << endl;
for (int i = 0; i < n; i++) {
cout << "Item " << i + 1 << ": " << fraction[i] * 100 << "%" << endl;
}
return 0;
}